package lab10;

import java.security.InvalidParameterException;

public class Person {
	private String name;
	private double weight;
	private int age;
	private Person[] friends;
	private boolean isMale;
	private Car car;
	private boolean hasCar;
	private int money;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		if (name!=null&&name.trim().length()>0)	
			this.name = name;
		else
			throw new InvalidParameterException("Empty name");
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		if (weight>=0)
			this.weight = weight;
		else
			throw new InvalidParameterException("Negative weight");
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		if (age >= 0)
			this.age = age;
		else
			throw new InvalidParameterException("Negative age");
	}

	public Person[] getFriends() {
		return friends;
	}

	public void setFriends(Person[] friends) {
		if (friends!=null)
			this.friends = friends;
		else
			throw new InvalidParameterException("Friends is null");
	}

	public boolean isMale() {
		return isMale;
	}

	public void setMale(boolean isMale) {
		this.isMale = isMale;
	}

	public Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}

	public boolean getHasCar() {
		return hasCar;
	}

	public void setHasCar(boolean hasCar) {
		this.hasCar = hasCar;
	}

	public int getMoney() {
		return money;
	}

	public void setMoney(int money) {
		this.money = money;
	}

	public Person(String name, double weight, int age, Person[] friends, boolean isMale) {
		super();
		this.name = name;
		this.weight = weight;
		this.age = age;
		this.friends = friends;
		this.isMale=isMale;
		this.hasCar=false;
		this.money=0;
	}
	
	public Person() {
		this("",4.0,0,new Person[0], true);
	}
	
	public Person(String name, boolean isMale) {
		this(name, 4.0,0,new Person[3], isMale);
	}
	
	void buyCar (Car car) {
		if (car.getPrice()<money) {
			this.hasCar=true;
			this.car=car;
			money-=car.getPrice();
			car.setOwner(this);
		}
		else
			throw new InvalidParameterException("The car is too expensive");
	}
	
	void sellCarForScrap() {
		if (hasCar) {
			money+=car.calculateCarPriceForScrap();
			car=null;
			hasCar=false;
		}
		else
			throw new NullPointerException("No car to sell.");
	}

}
